require(['grid', 'topBar', 'date'], function () {
	var editDialog, formDialog;
	var beginTime, endTime;
    $('#top').topBar({
        left: [{
                   type: 'filter-more',
                   dateType: 'custom',
                   search: function () {
                	   search();
                   },
                   item: [
                       {
                           text: '时间快捷',
                           name1: 'beginTime',
                           name2: 'endTime',
//                           hidden:true,
                           type: 'dateLine'
                       },
                       {
                           text: '故障单号',
                           name: 'rcrdId',
                           width: '140',
                           type: 'text'
                       },
                       {
                           text: '故障名称',
                           name: 'mftTypeName',
                           width: '140',
                           type: 'text'
                       }
                   ]
               }
        ],
        right: [
            {
                text: '导出',
                type: 'export-btn',
                authority: '',
                item: [
                    {
                        text: 'Excel导出',
                        click: function () {
                        	exportFile('Excel');
                        }
                    },
                    {
                        text: 'PDF导出',
                        click: function () {
                        	exportFile('Pdf');
                        }
                    }
                ]
            }
        ]
    });
    
    function exportFile(suffix){
    	var $postData=$("#top").parent().getValues();
		if($('#grid').getGridParam("reccount")<1){
			$.messager.alert("提示","请先查询数据。","info");
			return;
		}
		$postData.statisType = 'CAR';
		var type = commonFun.getLeftTreeCurrentTreeType();
		var orgIds = commonFun.getLeftTreeCheckedNodes(type);
		if(!orgIds){
			$.messager.alert("提示","请选择查询车辆/机构/驾驶员！","info");
			return;
		}
    	$postData.type=type;
    	$postData.orgIds=orgIds;
    	var url = "/reportFaultHandle/exportReportFaultHandle"+suffix+".htm";
    	$.exportFile(url,$postData);		
    }
    
	$('#grid').parent().height($(window).height()-60);
    
    $('#grid').grid({
        multiselect:false,
        url : "/reportFaultHandle/searchReportFaultHandleDetail.htm",
//        tableId:'e063368fc8ad4d0ab011fc9a739ff1cd',
		colModel:[
			{name: "mftId", label:"mftId", hidden: true},
			{name: "lineId", label:"lineId", hidden: true},
			{name: "carId", label:"carId", hidden: true},
			{name: "organName", label:"所属机构",width:80},
			{name: "lineName", label:"运营线路",width:80},
			{name: "licPltNo", label:"车牌",width:80},
			{name: "eqmtCd", label:"VIN码",width:80},
			{name: "rcrdId", label:"故障单号",width:80},
			{name: "crtTime", label:"生成时间",width:80},
			{name: "pcsTime", label:"更新时间",width:80},
			{name: "pcsSt", label:"故障单状态",width:80, dict:"PCSS0000"},
			{name: "mftTypeName", label:"故障名称",width:80},
			{name: "mftOccurTime", label:"故障发生时间",width:80},
			{name: "mftRepTime", label:"故障修复时间",width:80},
			{name: "operate", width: 130, label: '操作', title: false,align:'center',
                formatter: 'operArea',
                formatoptions: [{
                    text: '编辑',
                    css:'operate-edit',
                    click: function (data) {
                    	 getEditDialog('edit','编辑').dialog('exec', 'setVal', data.mftId, data.mftOccurTime.substring(0, 10), 
                    			 data.mftOccurTime.substring(0, 10)+" 23:59:59", data.rcrdId, data.crtTime, data.pcsTime, 
                    			 data.licPltNo, data.organName, data.eqmtCd);
                    }
                },{
                    text: '查看故障单',
                    css:'operate-scan',
                    click: function (data) {
                    	 getFormDialog('','故障单信息('+data.pcsSt+")").dialog('exec', 'getFormInfo', data.mftId, data.carId, beginTime, endTime);
                    }
                }]
            }
		]
	});
    
    
    function search(){
 	   var $postData=$("#top").parent().getValues();
		$postData.statisType = 'CAR';
 	   var type =  commonFun.getLeftTreeCurrentTreeType();
	   var orgIds = commonFun.getLeftTreeCheckedNodes(type);
		if(!orgIds){
			$.messager.alert("提示","请选择查询车辆/机构/驾驶员。","info");
			return;
		}
		$postData.type=type;
		$postData.orgIds=orgIds;
		//用于弹出层日期条件
		beginTime = $postData.beginTime;
		endTime = $postData.endTime;
		
		$('#grid').grid('setGridParam', {
			page:1,
			datatype: "json",
			postData: $postData
		}).trigger("reloadGrid");
    }

    function getEditDialog(flag,option) {
    	funName = flag;
        option = option || {};
        if (editDialog) {
            editDialog.dialog('open');
            if (option.title) {
                editDialog.dialog('setOption', 'title', option.title);
            }
        } else {
            editDialog = $('<div>').dialog({
                title: option.title || '车辆信息维护',
                url: './safety/report/reportFaultHandleDetailEdit.htm',
                height: 550,
                width: 720,
                autoOpen: true,
                buttons: [
                    {
                        text: '确定',
                        css: 'mr10',
                        click: function () {
                        	var _t = this;
                        	 //funName :  add or edit
                        	 $(_t).dialog('exec', funName,function(){
                 		    	reload(true);
                 		    });
                        }
                    },
                    {
                        text: '重置',
                        css: 'btn-cancel',
                        click: function () {
                        	$(this).dialog('exec', 'reset');
                        }
                    }
                ]
            });
        }
        return editDialog;
    }
    
  //新增修改后执行
	function reload(close){
		search();
    	close && editDialog.dialog('close');
	};
    
    
    function getFormDialog(flag,option) {
    	funName = flag;
        option = option || {};
        if (formDialog) {
        	formDialog.dialog('open');
            if (option.title) {
            	formDialog.dialog('setOption', 'title', option.title);
            }
        } else {
        	formDialog = $('<div>').dialog({
                title: option.title || '车辆信息维护',
                url: './safety/report/faultHandleFormInfo.htm',
                height: 550,
                width: 720,
                autoOpen: true,
                buttons: [
                    {
                        text: '关闭',
                        css: 'btn btn-cancel btn-medium',
                        click: function () {
                            $(this).dialog('close');
                        }
                    }
                ]
            });
        }
        return formDialog;
    }
    
    
});
